package com.evan.webadmin.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evan.common.core.constant.MaterialTypeEnum;
import com.evan.common.core.constant.ModuleTypeEnum;
import com.evan.common.core.constant.PersonnelTypeEnum;
import com.evan.webadmin.biz.config.BizConfig;
import com.evan.webadmin.biz.dao.MaterialMapper;
import com.evan.webadmin.biz.dto.MaterialDto;
import com.evan.webadmin.biz.dto.MaterialFilter;
import com.evan.webadmin.biz.model.Material;
import com.evan.webadmin.biz.service.IMaterialService;
import com.evan.webadmin.biz.vo.MaterialFrontVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Pavel
 * @since 2024-06-09
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    BizConfig bizConfig;

    @Override
    public List<Material> findByCondition( MaterialFilter materialFilter) {

        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(materialFilter.getMaterialTitle())){
            wrapper.likeRight("material_title", materialFilter.getMaterialTitle());
        }
        if(materialFilter.getPersonnelType() != null && materialFilter.getPersonnelType() != PersonnelTypeEnum.DEFAULT.getCode()){
            wrapper.eq("personnel_type", materialFilter.getPersonnelType());
        }
        if(materialFilter.getModuleType() != null && materialFilter.getModuleType() != ModuleTypeEnum.DEFAULT.getCode()){
            wrapper.eq("module_type", materialFilter.getModuleType());
        }
        if(materialFilter.getMaterialType() != null){
            wrapper.eq("material_type", materialFilter.getMaterialType());
        }
        if(materialFilter.getSceneType() != null){
            wrapper.eq("scene_type", materialFilter.getSceneType());
        }
        wrapper.eq("delete_flag", 0L);
        return materialMapper.selectList(wrapper);
    }

    @Override
    public void edit(MaterialDto materialDto) {
        Material material = null;
        if(materialDto.getMaterialId() != null){
            material = materialMapper.selectByMaterialId(materialDto.getMaterialId());
        }else{
            material = new Material();
        }
        material.setMaterialContent(materialDto.getMaterialContent());
        material.setMaterialTitle(materialDto.getMaterialTitle());
        material.setMaterialType(materialDto.getMaterialType());
        material.setMaterialFileUrl(materialDto.getMaterialFileUrl());
        material.setPersonnelType(materialDto.getPersonnelType());
        material.setSceneType(materialDto.getSceneType());
        material.setModuleType(materialDto.getModuleType());
        if(material.getMaterialId() != null){
            material.setUpdateTime(new Date());
            materialMapper.update(material, new QueryWrapper<Material>().eq("material_id", material.getMaterialId()));
        }else{
            material.setCreateTime(new Date());
            materialMapper.insert(material);
        }
    }

    @Override
    public Material findById(Long materialId) {
        return materialMapper.selectByMaterialId(materialId);
    }

    @Override
    public List<MaterialFrontVo> findByFront(MaterialFilter materialFilter) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("delete_flag", 0L);
        List<Material> materials = new ArrayList<>();
        if(materialFilter.getModuleType() == ModuleTypeEnum.COURSE.getCode()){
            wrapper.eq("module_type", ModuleTypeEnum.COURSE.getCode());
            wrapper.eq("scene_type", materialFilter.getSceneType());
            wrapper.eq("material_type", materialFilter.getMaterialType());
            materials = materialMapper.selectList(wrapper);
        }else if(materialFilter.getModuleType() == ModuleTypeEnum.MATERIAL.getCode()){
            wrapper.eq("module_type", ModuleTypeEnum.MATERIAL.getCode());
            wrapper.eq("personnel_type", materialFilter.getPersonnelType());
            materials = materialMapper.selectList(wrapper);
        }
        return assemblyFront(materials);
    }

    private List<MaterialFrontVo> assemblyFront(List<Material> materials) {
        List<MaterialFrontVo> results = new ArrayList<>();
        for(Material material : materials){
            MaterialFrontVo materialFrontVo = new MaterialFrontVo();
            materialFrontVo.setMaterialId(material.getMaterialId());
            materialFrontVo.setMaterialContent(material.getMaterialContent());
            materialFrontVo.setMaterialTitle(material.getMaterialTitle());
            materialFrontVo.setMaterialType(material.getMaterialType());
            if(StringUtils.isNotBlank(material.getMaterialFileUrl())){
                if(MaterialTypeEnum.DOC.getCode() == material.getMaterialType()){
                    String videoOriginalUrl = bizConfig.getDownloadPrefix() + material.getMaterialFileUrl();
                    String encodedString = Base64.getEncoder().encodeToString(videoOriginalUrl.getBytes(StandardCharsets.UTF_8));
                    String urlEncodedString = null;
                    try {
                        urlEncodedString = URLEncoder.encode(encodedString, StandardCharsets.UTF_8.toString());
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    if(StringUtils.isNotBlank(urlEncodedString)){
                        materialFrontVo.setMaterialPreviewUrl(MessageFormat.format(bizConfig.getPreviewTemplate(), urlEncodedString));
                    }
                }else{
                    materialFrontVo.setMaterialPreviewUrl(bizConfig.getDownloadPrefix() + material.getMaterialFileUrl());
                }
            }
            results.add(materialFrontVo);
        }
        return results;
    }

    @Override
    public void logicDelete(Long materialId) {
        Material material = materialMapper.selectByMaterialId(materialId);
        material.setDeleteFlag(System.currentTimeMillis());
        material.setUpdateTime(new Date());
        materialMapper.update(material, new QueryWrapper<Material>().eq("material_id", materialId));
    }
}
